package com.sxit.tdf.server.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxit.tdf.server.entity.File;
import com.sxit.tdf.server.entity.FileApprove;
import com.sxit.tdf.server.entity.FileBucket;
import com.sxit.tdf.server.entity.FilePath;
import com.sxit.tdf.server.entity.bo.FileApproveInfoBo;
import com.sxit.tdf.server.entity.vo.InsertApproveFileVo;
import com.sxit.tdf.server.exception.TdfException;
import com.sxit.tdf.server.mapper.FileApproveMapper;
import com.sxit.tdf.server.mapper.FileMapper;
import com.sxit.tdf.server.service.IFileApproveService;
import com.sxit.tdf.server.service.IFileBucketService;
import com.sxit.tdf.server.service.IFilePathService;
import com.sxit.tdf.server.util.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 服务实现类
 *
 * @author 橙子高
 * @since 2022-03-19
 */
@Service
public class FileApproveServiceImpl extends ServiceImpl<FileApproveMapper, FileApprove>
    implements IFileApproveService {

  @Autowired private IFileBucketService bucketService;
  @Autowired private IFilePathService filePathService;
  @Autowired private FileMapper fileMapper;

  @Override
  @Transactional
  public Boolean upLoadApproveFile(InsertApproveFileVo insertApproveFileVo, MultipartFile file)
      throws IOException {

    // 获取筒信息
    FileBucket bucket = bucketService.getById(insertApproveFileVo.getFileBucketId());
    // 获取文件夹信息
    FilePath filePath = filePathService.getById(insertApproveFileVo.getFilePathId());
    // 获取文件信息
    String filename = file.getOriginalFilename();
    // 获取文件信息
    String ext = filename.substring(filename.lastIndexOf(".") + 1);
    // 生成文件的随机名称
    String uuidFileName = UUID.fastUUID() + "." + ext;

    // 上传文件
    File f = new File();
    f.setFileDownload(0);

    f.setFileType("3");
    f.setFileRealName(filename);
    f.setFileDescribe(insertApproveFileVo.getFileDescribe());
    f.setFileOssPath(
        "https://"
            + bucket.getBucketCode()
            + ".oss-cn-beijing.aliyuncs.com/"
            + filePath.getPathCode()
            + uuidFileName);
    f.setFileName(insertApproveFileVo.getFileName());
    f.setFileSuffix(ext);
    f.setFilePathId(insertApproveFileVo.getFilePathId());
    f.setFileBucketId(insertApproveFileVo.getFileBucketId());
    f.setIsDelete(0);
    f.setCreateTime(LocalDateTime.now());
    f.setFileOwnerId(StpUtil.getLoginIdAsString());
    f.setFileOwnerId(StpUtil.getLoginIdAsString());
    int insert = fileMapper.insert(f);
    // 上传到OSS
    if (insert == 1) {
      new OSSUtil()
          .uploadFile(
              bucket.getBucketCode(), filePath.getPathCode(), uuidFileName, file.getBytes());

    } else {
      throw new TdfException("文件上传错误");
    }

    // 记录审批信息
    // 是否有审批权限
    boolean b = StpUtil.hasPermission("DOCUMENT_APPROVAL:APPROVAL");

    FileApprove fileApprove = new FileApprove();

    if (b) {
      fileApprove.setFileState(2);
      fileApprove.setFileCause("");
      fileApprove.setFileApprover(StpUtil.getLoginIdAsString());
      fileApprove.setFinalTime(LocalDateTime.now());
      fileApprove.setCreateTime(LocalDateTime.now());

    } else {
      fileApprove.setFileState(1);
      fileApprove.setCreateTime(LocalDateTime.now());
      fileApprove.setUpdateTime(LocalDateTime.now());
    }

    fileApprove.setFileId(f.getId());
    fileApprove.setFileTimes(0);

    fileApprove.setFileOwner(StpUtil.getLoginIdAsString());
    int insert1 = baseMapper.insert(fileApprove);

    return insert1 == 1;
  }

  @Override
  public List<FileApproveInfoBo> getPrivateApproveFileList() {
    String loginIdAsString = StpUtil.getLoginIdAsString();
    return baseMapper.getPrivateApproveFileList(loginIdAsString);
  }

  @Override
  public void cancelFileApprove(String fileId, String loginIdAsString) {

    baseMapper.cancelFileApprove(fileId, loginIdAsString);
  }

  @Override
  public List<FileApproveInfoBo> getDeptApproverList(Integer deptId) {
    return baseMapper.getDeptApproverList(deptId);
  }

  @Override
  public Boolean reUpLoadApproveFile(InsertApproveFileVo insertApproveFileVo, MultipartFile file)
      throws IOException {

    // 获取筒信息
    FileBucket bucket = bucketService.getById(insertApproveFileVo.getFileBucketId());
    // 获取文件夹信息
    FilePath filePath = filePathService.getById(insertApproveFileVo.getFilePathId());
    // 获取文件信息
    String filename = file.getOriginalFilename();
    // 获取文件信息
    String ext = filename.substring(filename.lastIndexOf(".") + 1);
    // 生成文件的随机名称
    String uuidFileName = UUID.fastUUID() + "." + ext;

    // 上传文件
    File f = new File();
    f.setFileDownload(0);

    f.setFileType("3");
    f.setFileRealName(filename);
    f.setFileDescribe(insertApproveFileVo.getFileDescribe());
    f.setFileOssPath(
        "https://"
            + bucket.getBucketCode()
            + ".oss-cn-beijing.aliyuncs.com/"
            + filePath.getPathCode()
            + uuidFileName);
    f.setFileName(insertApproveFileVo.getFileName());
    f.setFileSuffix(ext);
    f.setFilePathId(insertApproveFileVo.getFilePathId());
    f.setFileBucketId(insertApproveFileVo.getFileBucketId());
    f.setIsDelete(0);
    f.setCreateTime(LocalDateTime.now());
    f.setFileOwnerId(StpUtil.getLoginIdAsString());
    f.setFileOwnerId(StpUtil.getLoginIdAsString());
    int insert = fileMapper.insert(f);
    // 上传到OSS
    if (insert == 1) {
      new OSSUtil()
          .uploadFile(
              bucket.getBucketCode(), filePath.getPathCode(), uuidFileName, file.getBytes());

    } else {
      throw new TdfException("文件上传错误");
    }

    FileApprove old = baseMapper.selectById(insertApproveFileVo.getId());

    // 记录审批信息
    FileApprove fileApprove = new FileApprove();
    fileApprove.setId(insertApproveFileVo.getId());
    fileApprove.setFileState(1);
    fileApprove.setFileId(f.getId());
    fileApprove.setFileTimes(old.getFileTimes() + 1);
    fileApprove.setFileState(1);
    fileApprove.setFileOwner(StpUtil.getLoginIdAsString());

    fileApprove.setUpdateTime(LocalDateTime.now());

    int insert1 = baseMapper.updateById(fileApprove);

    return insert1 == 1;
  }
}
